package yelmor.comm100.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import yelmor.comm100.domain.OfMessageArchive;
import yelmor.comm100.repository.CoUserRepository;
import yelmor.comm100.repository.OfMessageArchiveRepository;
import yelmor.comm100.util.SecurityUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

/**
 * Created by yel on 2016/5/5.
 */
@Controller
public class MessageHistoryController {

  @Autowired
  private CoUserRepository coUserRepository;

  @Autowired
  private OfMessageArchiveRepository ofMessageArchiveRepository;

  @Value("${openfire.host}")
  private String openfireHost;

  @RequestMapping(path = "/user/serviceMessage/{serviceName}")
  @ResponseBody
  public String serviceMessageHistory(@PathVariable String serviceName, Model model) {
    String userName = SecurityUtil.getCurrentUserName();
    String siteCode = coUserRepository.findOne(userName).getSiteCode();
    String serviceJid = serviceName + "-service-" + siteCode + "@" + openfireHost;
    ArrayList<OfMessageArchive> messageItems = (ArrayList<OfMessageArchive>) ofMessageArchiveRepository.findByFromJIDOrToJID(serviceJid, serviceJid);
    System.out.println("get " + messageItems.size());
    String messageContent = "";
    System.out.println(messageItems.get(1).toString());
    return messageItems.get(0).toString();
  }
/*

  @RequestMapping(path = "/user/serviceMessagee/{serviceName}")
  @ResponseBody
  public String serviceMessageHistorye(@PathVariable String serviceName, Model model) {
    SecurityUtil.getCurrentUserName();
    ArrayList<OfMessageArchive> messageItems = (ArrayList<OfMessageArchive>) ofMessageArchiveRepository.findTest2("jack-b-service-zkbtmg3p@zy48");
    System.out.println("get " + messageItems.size());
    */
/*String messageContent = "";
    for(Iterator<OfMessageArchive> iter = messageItems.iterator(); iter.hasNext();) {
      OfMessageArchive ofmessageArchive = iter.next();
      messageContent += ofmessageArchive.getFromJID() + " to: " + ofmessageArchive.getToJID() + "时间" +  new Date(ofmessageArchive.getSentDate()) + "\n";
      messageContent += ofmessageArchive.getBody() + "\n";
    }*//*

    return "query";
  }
*/

  @RequestMapping(path = "/user/serviceMessageee/{serviceName}")
  @ResponseBody
  public String serviceMessageHistoryee(@PathVariable String serviceName, Model model) {
    SecurityUtil.getCurrentUserName();
    ArrayList<OfMessageArchive> messageItems = (ArrayList<OfMessageArchive>) ofMessageArchiveRepository.findWithJid2("jack-b-service-zkbtmg3p@zy48");
    System.out.println("get " + messageItems.size());
    /*String messageContent = "";
    for(Iterator<OfMessageArchive> iter = messageItems.iterator(); iter.hasNext();) {
      OfMessageArchive ofmessageArchive = iter.next();
      messageContent += ofmessageArchive.getFromJID() + " to: " + ofmessageArchive.getToJID() + "时间" +  new Date(ofmessageArchive.getSentDate()) + "\n";
      messageContent += ofmessageArchive.getBody() + "\n";
    }*/
    return "query";
  }



}
